# -*- coding: utf-8 -*-
# @Date   : 2020/3/20
# @File   : R63_EntInventoryLoanFiveClassCheck_CF.py
# @Author : zhaochen
# -*- coding: utf-8 -*-

'''eof
name:申请企业存量业务五级分类校验
code:R63_EntInventoryLoanFiveClassCheck_CF
tableName:
columnName:
groups:业务其他规则校验
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime
reload(sys)
sys.setdefaultencoding('utf-8')


def get_age(str1):
    try:
        date1 = datetime.now()
        date2 = date_format(str1)
        diff_year = date1.year - date2.year
        diff_month = date1.month - date2.month
        diff_day = date1.day - date2.day
        age = diff_year + diff_month/12.0 + diff_day/365.0
        return age
    except:
        return u'缺失值'


# 统一转成datetime对象，只能转年月日的字符串
def date_format(time):
    import re
    r = re.compile(r'\D+')
    try:
        date = datetime.strptime(time, '%Y.%m.%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d')
                except:
                    date = None
    return date


def float_check(target):
    try:
        float(target)
        return True
    except:
        return False


def R63_EntInventoryLoanFiveClassCheck_CF():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    tweleveclassifyresult_list = ['B1', 'B2', 'B3', 'C1', 'C2', 'D1', 'D2', 'E']
    abnormalloan = json.loads(ratingRequest.get("abNormalloan"))
    resultList = []
    if abnormalloan not in null_type_list and isinstance(abnormalloan, list):
        if len(abnormalloan) >= 1:
            flag = False
            for i in abnormalloan:
                tweleveclassifyresult = i.get("tweleveClassifyResult")
                customertype = i.get("customerType")
                businessType = i.get("businessType")
                if customertype not in null_type_list:
                    if customertype == "1":
                        if businessType in ("107010", "107055", "10101025"):
                            resultList.append(businessType)
                            continue
                        resultList.append(tweleveclassifyresult)
                else:
                    resultList.append(u"customertype为空")
            return u','.join(resultList)
        else:
            return 1
    else:
        raise RuntimeError("网贷传输的数据格式错误或者为空")


if __name__ == "__main__":
    ratingRequest = {"legalPersonIdCard":"500101199202192411","timeLimit":"12","abNormalloan": [{"tweleveClassifyResult": "B3", "businesstype":u"个人业务", "customerType":"1"},{"balance": "30", "businesstype":u"个业务", "customertype":"1"}]}
    aa = R63_EntInventoryLoanFiveClassCheck_CF()
    print aa


result = R63_EntInventoryLoanFiveClassCheck_CF()